#!/usr/bin/python3
# -*- coding: utf-8 -*-

import pandas as pd
import numpy  as np
import talib as ta

eps = 1e-8

def signal(*args):
    # Fi
    df = args[0]
    n = args[1]
    factor_name = args[2]

    _fi = df['volume'] * (df['close'] - df['close'].shift(1))
    _fi_zscore = (_fi - _fi.rolling(n, min_periods=1).mean()) / \
                 (_fi.rolling(n, min_periods=1).std() + eps)
    signal = _fi_zscore.ewm(span=n, adjust=False, min_periods=1).mean()
    df[factor_name] = pd.Series(signal)

    return df

